package cn.com.commons.utils;

import java.util.Collection;

import org.apache.commons.lang3.StringUtils;

/**
 * 字符串工具类
 * 
 * @author Microbun
 * @version 2012-2-9
 * @since 1.0
 */
public class StringUtil extends StringUtils {

	/**
	 * 将十进制转为指定长度的16进制
	 * 
	 * @param num
	 *            要转化的数字
	 * @param width
	 *            16进制长度
	 * @return
	 */
	public static String toHexString(int num, int width) {
		String hex = Integer.toHexString(num);
		int lenght = hex.length();
		StringBuilder str = new StringBuilder("0x");
		int i = 0;
		while ((width - (lenght + i)) >= 0) {
			str.append("0");
			i++;
		}
		str.append(hex);
		return str.toString();
	}

	/**
	 * 去掉后缀
	 * 
	 * @param str
	 * @return
	 */
	public static String removeSuffix(String str) {
		int suffixIndex = str.indexOf(".");
		return str.substring(0, suffixIndex);
	}

	/**
	 * 将所有的斜杠转换为点
	 * 
	 * @param str
	 * @return
	 */
	public static String convertSlashToDot(String str) {
		return str.replaceAll("/", ".");
	}
	

	/**
	 * 用指定的字符串将集合连接并拼装为字符串
	 * 
	 * @param collection
	 * @param str
	 * @return
	 */
	public static <E> String join(Collection<E> collection, String str) {
		StringBuilder sb = null;
		for (E e : collection) {
			if (sb == null) {
				sb = new StringBuilder(e.toString());
			} else {
				sb.append(str).append(e.toString());
			}
		}
		return sb.toString();
	}

}
